//
//  DayPicker_CellTableViewCell.m
//  car
//
//  Created by chenxu on 4/27/15.
//  Copyright (c) 2015 chenxu. All rights reserved.
//

#import "DayPicker_CellTableViewCell.h"
#define currentMonth [currentMonthString integerValue]

@implementation DayPicker_CellTableViewCell
{
    
        
        NSMutableArray *yearArray;
        NSArray *monthArray;
        NSMutableArray *DaysArray;
    
        NSString *currentMonthString;
        
        NSInteger selectedYearRow;
        NSInteger selectedMonthRow;
        NSInteger selectedDayRow;
        
        BOOL firstTimeLoad;
        
    
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSLog(@"加载了");
        _customPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
        [_customPicker setDelegate:self];
        [_customPicker setDataSource:self];
        [super addSubview:_customPicker];
        //_customPicker.hidden = YES;
        _customPicker.frame = CGRectMake(0, kScreen_Height-100, kScreen_Width, 100);

      //  _customPicker.frame = CGRectMake(0, kScreen_Height-100, kScreen_Width, 100);
    }
    return self;
}
- (void)awakeFromNib {
    // Initialization code
    NSLog(@"加载了");
    _customPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
    [_customPicker setDelegate:self];
    [_customPicker setDataSource:self];
    [self addSubview:_customPicker];
    //_customPicker.hidden = YES;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


- (void)settitle:(NSString*)str{
    _title.text = str;
}
- (IBAction)selectBtn:(id)sender {
    _customPicker.hidden = NO;
    if (self.timeSelectedBlock) {
        _timeSelectedBlock(_button.titleLabel.text);
    }
}

#pragma mark - UIPickerViewDelegate


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    
    if (component == 0)
    {
        selectedYearRow = row;
        [self.customPicker reloadAllComponents];
    }
    else if (component == 1)
    {
        selectedMonthRow = row;
        [self.customPicker reloadAllComponents];
    }
    else if (component == 2)
    {
        selectedDayRow = row;
        
        [self.customPicker reloadAllComponents];
        
    }
    _button.titleLabel.text = [NSString stringWithFormat:@"%ld",(long)selectedMonthRow ];
    
}


#pragma mark - UIPickerViewDatasource

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           reusingView:(UIView *)view {
    
    // Custom View created for each component
    
    UILabel *pickerLabel = (UILabel *)view;
    
    if (pickerLabel == nil) {
        CGRect frame = CGRectMake(0.0, 0.0, 50, 60);
        pickerLabel = [[UILabel alloc] initWithFrame:frame];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:15.0f]];
    }
    
    
    
    if (component == 0)
    {
        pickerLabel.text =  [yearArray objectAtIndex:row]; // Year
    }
    else if (component == 1)
    {
        pickerLabel.text =  [monthArray objectAtIndex:row];  // Month
    }
    else if (component == 2)
    {
        pickerLabel.text =  [DaysArray objectAtIndex:row]; // Date
        
    }
  
    
    return pickerLabel;
    
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    return 3;
    
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    if (component == 0)
    {
        return [yearArray count];
        
    }
    else if (component == 1)
    {
        return [monthArray count];
    }
    else 
    { // day
        
        if (firstTimeLoad)
        {
            if (currentMonth == 1 || currentMonth == 3 || currentMonth == 5 || currentMonth == 7 || currentMonth == 8 || currentMonth == 10 || currentMonth == 12)
            {
                return 31;
            }
            else if (currentMonth == 2)
            {
                int yearint = [[yearArray objectAtIndex:selectedYearRow]intValue ];
                
                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
                    
                    return 29;
                }
                else
                {
                    return 28; // or return 29
                }
                
            }
            else
            {
                return 30;
            }
            
        }
        else
        {
            
            if (selectedMonthRow == 0 || selectedMonthRow == 2 || selectedMonthRow == 4 || selectedMonthRow == 6 || selectedMonthRow == 7 || selectedMonthRow == 9 || selectedMonthRow == 11)
            {
                return 31;
            }
            else if (selectedMonthRow == 1)
            {
                int yearint = [[yearArray objectAtIndex:selectedYearRow]intValue ];
                
                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
                    return 29;
                }
                else
                {
                    return 28; // or return 29
                }
                
                
                
            }
            else
            {
                return 30;
            }
            
        }
        
        
    }
    
    
}
@end
